package com.hardroid.workload.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;

import com.hardroid.workload.R;
import com.hardroid.workload.ui.fragment.QuestControlFragment;
import com.hardroid.workload.ui.fragment.QuestPanelFragment;
import com.hardroid.workload.utils.IObsvFragmentReplacable;
import com.hardroid.workload.utils.Singleton;

/**
 * 
 * Activity buat menangani pengisian kuesioner
 * @author hartono
 *
 */
public class QuestionaireActivity extends Activity implements IObsvFragmentReplacable{
	
	public QuestControlFragment controlFragment;
	public QuestPanelFragment panelFragment;

	
	public AlertDialog inputDialog;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_questionaire);
		
		Log.i("Questionaire Acitivy", "onCreATE");
		controlFragment = (QuestControlFragment) getFragmentManager()
				.findFragmentById(R.id.fragment_quest_control);
		panelFragment = (QuestPanelFragment) getFragmentManager()
				.findFragmentById(R.id.fragment_questionaire);

		if (panelFragment == null) {
			if (!Singleton.currentProject.lEmployer.isEmpty()) {
				Log.i("Questionaire Acitivy", "onCreate Questionaire Instance ");
				try{
					panelFragment = QuestPanelFragment.createInstance(0,Singleton.currentProject.lEmployer.get(0).sheet.lObsvDay.size()-1);
				}catch(Exception e){
					panelFragment = QuestPanelFragment.createInstance(0, 0);
				}
				// Execute a transaction, replacing any existing fragment
				// with this one inside the frame.
				Log.i("Questionaire Acitivy", "ft . begin");
				FragmentTransaction ft = getFragmentManager()
						.beginTransaction();
				ft.replace(R.id.fragment_questionaire, panelFragment);
				ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
				ft.commit();
				Log.i("Questionaire Acitivy", "ft . commit");
			}
		}
		if (controlFragment!=null){
			controlFragment.setListEmployer(Singleton.currentProject.lEmployer);
		}		
		
	}
	
	@Override
	public void replaceFragment(int id, int did) {
		// TODO Auto-generated method stub
		Log.i("Questionaire Acitivy", "replace fragment "+id + ","+did);
		
		if (panelFragment.employerId != id ){
			Log.i("Questionaire Acitivy", "employer - replace fragment create instance"+id);
			panelFragment = QuestPanelFragment.createInstance(id,did);
	
			// Execute a transaction, replacing any existing fragment
			// with this one inside the frame.
			Log.i("Questionaire Acitivy", "replace fragment fr begin "+id);
			FragmentTransaction ft = getFragmentManager().beginTransaction();
			ft.replace(R.id.fragment_questionaire, panelFragment);
			ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
			ft.commit();
			Log.i("Questionaire Acitivy", "replace fragment fr end "+id);
		}else{
			if (panelFragment.dayId!=did){
				Log.i("Questionaire Acitivy", "day - replace fragment create instance"+did);
				panelFragment = QuestPanelFragment.createInstance(id,did);
		
				// Execute a transaction, replacing any existing fragment
				// with this one inside the frame.
				Log.i("Questionaire Acitivy", "replace fragment fr begin "+did);
				FragmentTransaction ft = getFragmentManager().beginTransaction();
				ft.replace(R.id.fragment_questionaire, panelFragment);
				ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
				ft.commit();
				Log.i("Questionaire Acitivy", "replace fragment fr end "+did);
			}else{
				Log.i("Questionaire Acitivy", "no replace");
			}
		}
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Singleton.saveProject(Singleton.currentProject);
	}
	
}
